import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { render } from "/@/utils/common/renderUtils"
import { UploadBizType } from "/@/enums/uploadEnum"
import { DescItem } from "/@/components/Description/index"

export const columns: BasicColumn[] = [
  {
    title: "头像",
    dataIndex: "avatar",
    width: 80,
    customRender: (record : any) => {
      return render.renderAvatar(record.avatar)
    },
  },
  {
    title: "用户账号",
    dataIndex: "userName",
    width: 130,
  },
  {
    title: "邮箱",
    dataIndex: "email",
    width: 160,
  },
  {
    title: "手机号",
    dataIndex: "phone",
    width: 100,
  },
  {
    title: "状态",
    dataIndex: "status",
    customRender: ({ text }) => {
      let color = "green"
      if (text === "2") {
        color = "red"
      }
      return render.renderTag(render.renderDict(text, "user_status"), color)
    },
    width: 60,
  },
  {
    title: "注册时间",
    dataIndex: "createdAt",
    width: 160,
  },
]

export const recycleColumns: BasicColumn[] = [
  {
    title: "用户账号",
    dataIndex: "userName",
    width: 100,
  },
  {
    title: "用户姓名",
    dataIndex: "realName",
    width: 100,
  },
  {
    title: "头像",
    dataIndex: "avatar",
    width: 80,
    customRender: (record : any) => render.renderAvatar(record.avatar),
  },
  {
    title: "性别",
    dataIndex: "sex",
    width: 80,
    sorter: true,
    customRender: ({ text }) => {
      return render.renderDict(text, "sys_user_sex")
    },
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "账号",
    field: "account",
    component: "Input",
    componentProps: {
      placeholder: "用户名/手机号/邮箱",
    },
  },
  {
    label: "性别",
    field: "sex",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "sys_user_sex",
      placeholder: "请选择性别",
      stringToNumber: true,
    },
  },
  {
    label: "真实名称",
    field: "realName",
    component: "Input",
  },
  {
    label: "用户状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "user_status",
      placeholder: "请选择状态",
      stringToNumber: true,
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "用户账号",
    field: "userName",
    component: "Input",
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    // dynamicRules: ({model,schema}) =>rules.duplicateCheckRule("sys_user", "userName",model,schema,true),
  },

  {
    label: "登录密码",
    field: "password",
    component: "StrengthMeter",
    componentProps: {
      placeholder: "请输入",
      autocomplete: "false",
    },
    rules: [
      {
        required: true,
        message: "请输入登录密码",
      },
    ],
  },
  {
    label: "确认密码",
    field: "confirmPassword",
    component: "InputPassword",
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
  {
    label: "用户姓名",
    field: "realName",
    component: "Input",
  },
  {
    label: "头像",
    field: "avatar",
    component: "MyImageUpload",
    componentProps: {
      fileMax: 1,
      bizType: UploadBizType.AVATAR,
    },
  },
  {
    label: "生日",
    field: "birthday",
    component: "DatePicker",
  },
  {
    label: "性别",
    field: "sex",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "sys_user_sex",
      placeholder: "请选择",
      // stringToNumber: true
    },
  },
  {
    label: "邮箱",
    field: "email",
    component: "Input",
    rules: rules.rule("email", false),
    dynamicRules: ({ model, schema }) => {
      if (!model.id || (model.id && model.email?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return rules.rule("email", false)
    },
  },
  {
    label: "手机号码",
    field: "phone",
    component: "Input",
    dynamicRules: ({ model, schema }) => {
      if (!model.phone || (model.id && model.phone?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return [{ pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: "手机号码格式有误" }]
    },
  },
  {
    label: "座机",
    field: "telephone",
    component: "Input",
    rules: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: "请输入正确的座机号码" }],
  },
  {
    label: "备注",
    field: "remark",
    component: "InputTextArea",
    componentProps: {
      placeholder: "请输入备注",
      rows: 3,
    },
  },
]

export const formPasswordSchema: FormSchema[] = [
  {
    label: "用户账号",
    field: "userName",
    component: "Input",
    componentProps: { readonly: true },
  },
  {
    label: "",
    field: "id",
    show: false,
    component: "Input",
  },
  {
    label: "登录密码",
    field: "password",
    component: "StrengthMeter",
    componentProps: {
      placeholder: "请输入登录密码",
    },
    rules: [
      {
        required: true,
        message: "请输入登录密码",
      },
    ],
  },
  {
    label: "确认密码",
    field: "confirmPassword",
    component: "InputPassword",
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
]

/**
 * 搜索用户
 */
export const searchUserFormSchema: FormSchema[] = [
  {
    field: "userName",
    label: "用户账号",
    component: "Input",
    colProps: { span: 12 },
  },
  {
    field: "test",
    label: "",
    component: "Input",
    slot: "addUser",
    colProps: { span: 12 },
  },
]

export const detailSchema: DescItem[] = [
  {
    field: "avatar",
    label: "头像",
    span: 2,
    render: (curVal, data) => {
      return render.renderAvatar(curVal)
    },
  },
  {
    field: "userName",
    label: "用户账号"
  },
  {
    field: "email",
    label: "邮箱"
  },
  {
    field: "phone",
    label: "手机号"
  },
  {
    field: "birthday",
    label: "生日"
  },
  {
    field: "sex",
    label: "性别",
    render: (curVal, data) => {
      return render.renderDict(curVal, "sys_user_sex")
    },
  },
  {
    field: "status",
    label: "状态",
    render: (curVal, data) => {
      return render.renderTag(render.renderDict(curVal, "user_status"), curVal == '2' ? 'red' : 'green')
    },
  },
  {
    field: "createdBy",
    label: "创建者",
  },
  {
    field: "updatedBy",
    label: "更新者",
  },
  {
    field: "lastLoginIp",
    label: "最后登录IP",
  },
  {
    field: "lastLoginDate",
    label: "最后登录时间",
  },
  {
    field: "createdAt",
    label: "注册时间",
  },
  {
    label: "注册IP",
    field: "registerIp",
  },
  {
    field: "registerTerminalName",
    label: "注册终端",
    render: (curVal, data) => {
      return render.renderTag(curVal, 'green')
    },
    span: 2
  },
  {
    field: "remark",
    label: "备注",
    span: 2
  },
]
